package com.psbc.redislock.controller;

import cn.hutool.core.util.StrUtil;
import com.psbc.redislock.pojo.Result;
import com.psbc.redislock.service.impl.GoodsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@RestController
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsService;

    private AtomicInteger withOutLockCount = new AtomicInteger();
    private AtomicInteger jdkLockCount = new AtomicInteger();
    private AtomicInteger dbLockCount = new AtomicInteger();
    private AtomicInteger redisLockCount = new AtomicInteger();
    private AtomicInteger redisLockWatchCount = new AtomicInteger();
    private AtomicInteger redisLockAnnotCount = new AtomicInteger();

    @GetMapping("/init")
    public Object init() {
        withOutLockCount.set(0);
        redisLockCount.set(0);
        jdkLockCount.set(0);
        dbLockCount.set(0);
        redisLockWatchCount.set(0);
        redisLockAnnotCount.set(0);
        return "ok";
    }

    @PostMapping("/buyNoLock")
    public Result buyNoLock(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result result = goodsService.shopping(name, num);
        if (200 == result.getCode()) {
            int i = withOutLockCount.incrementAndGet();
            log.info("无锁成功数：{}", i);
        }
        return result;
    }

    @PostMapping("/buyWithJdkLock")
    public Result jdkLockCount(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result result = goodsService.shoppingWithJdkLock(name, num);
        if (200 == result.getCode()) {
            int i = jdkLockCount.incrementAndGet();
            log.info("jdk锁成功数：{}", i);
        }
        return result;
    }


    @PostMapping("/buyWithDbLock")
    public Result buyWithDbLock(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result result = goodsService.shoppingWithDbLock(name, num);
        if (200 == result.getCode()) {
            int i = dbLockCount.incrementAndGet();
            log.info("dblock成功数：{}", i);
        }
        return result;
    }


    @PostMapping("/buyWithRedisLock")
    public Result buyWithRedisLock(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result shopping = goodsService.shoppingWithRedisLock(name, num);
        if (200 == shopping.getCode()) {
            int i = redisLockCount.incrementAndGet();
            log.info("reids锁成功数：{}", i);
        }
        return shopping;
    }

    @PostMapping("/buyWithRedisLockAddWatchDod")
    public Result buyWithRedisLockAddWatchDod(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result shopping = goodsService.shoppingWithRedisLockAddWatchDod(name, num);
        if (200 == shopping.getCode()) {
            int i = redisLockWatchCount.incrementAndGet();
            log.info("reids锁Watchdog成功数：{}", i);
        }
        return shopping;
    }

    @PostMapping("/buyWithAnnotation")
    public Result buyWithAnnotation(@RequestParam String name, @RequestParam Integer num) throws Exception {
        if (StrUtil.isBlank(name) || num == null) {
            return Result.NO("不能为空！");
        }
        Result shopping = goodsService.shoppingWithAnnotation(name, num);
        if (200 == shopping.getCode()) {
            int i = redisLockAnnotCount.incrementAndGet();
            log.info("reids锁注解成功数：{}", i);
        }
        return shopping;
    }
}
